/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.lables.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.datasource.annotation.DataSource;
import io.renren.modules.lables.dao.UserTagMapper;
import io.renren.modules.lables.entity.UserTag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试多数据源
 *
 * @author Mark sunlightcs@gmail.com
 */
@Service
@DataSource("slave_3")
public  class UserTagService{
    @Autowired
    private UserTagMapper userTagMapper;

    public void insert(UserTag entity) {

        userTagMapper.insert(entity);

    }

    public List<UserTag> list() {
        return userTagMapper.selectList(null);
    }

    public void save(UserTag userTag) {
        userTagMapper.insert(userTag);
    }

    public void update(UserTag userTag) {
        QueryWrapper<UserTag> updateWrapper = new QueryWrapper<>();
        updateWrapper.eq("userid", userTag.getUserId())
                .eq("tagname",userTag.getTagName())
                .eq("discinct_id",userTag.getDistinctId())
                .eq("tagvalue",userTag.getTagValue())
                .eq("subject",userTag.getSubject())
                .eq("inserttime",userTag.getInsertTime());
        userTagMapper.update(userTag,updateWrapper);
    }


    public void remove(String distinctId) {
        QueryWrapper<UserTag> updateWrapper = new QueryWrapper<>();
        updateWrapper.eq("discinctId", distinctId);
        userTagMapper.delete(updateWrapper);
    }

    public List<UserTag> listByUserId(String userId) {
        QueryWrapper<UserTag> updateWrapper = new QueryWrapper<>();
        updateWrapper.eq("userid", userId);
        return userTagMapper.selectList(updateWrapper);
    }

    public void saveBatch(ArrayList<UserTag> userTags) {
        for (UserTag userTag : userTags) {
            userTagMapper.insert(userTag);
        }
    }

}